package com.shq.ex1;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class SimCalcuGesActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener {

    private EditText edit01 = null;
    private EditText edit02 = null;
    private TextView show = null;
    private TextView result = null;
    private Button button = null;
    private GestureLibrary library = null;
    private String operator = null;
    private RadioButton radio = null;
    private RadioGroup oper = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sim_calcu_ges);

        edit01 = (EditText)findViewById(R.id.jsqgEdit01);
        edit02 = (EditText)findViewById(R.id.jsqgEdit02);
        show = (TextView) findViewById(R.id.jsqgShow);
        result = (TextView) findViewById(R.id.jsqgResult);
        button = (Button)findViewById(R.id.jsqgButton);
        oper = (RadioGroup)findViewById(R.id.jsqgoper);

        library = GestureLibraries.fromRawResource(this,R.raw.gestures);

        GestureOverlayView gesture = (GestureOverlayView)findViewById(R.id.jsqggesture);
        gesture.addOnGesturePerformedListener(this);

        if(!library.load()){
            finish();//手势文件加载失败则退出
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(radio != null ){
                    operator = radio.getText().toString();
                    show.setText("您的运算式为:"+edit01.getText()+operator+edit02.getText());
                    if(edit01.getText()!=null && edit02.getText()!=null){
                        String num01 = edit01.getText().toString();
                        String num02 = edit02.getText().toString();
                        int num1 = Integer.parseInt(num01);
                        int num2 = Integer.parseInt(num02);
                        result.setText("计算结果为："+getResult(operator,num1,num2));
                    }
                }
            }
        });
        //单选按钮监听事件
        oper.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                radio = (RadioButton)findViewById(checkedId);
            }
        });
    }

    private int getResult(String operator, int num1, int num2) {
        int result = -1;
        if(radio.getText().toString().equals("+")){
            result = num1 + num2;
        }else{
            result = num1 - num2;
        }
        return result;
    }

     public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture){
         ArrayList<Prediction> gestures = library.recognize(gesture);

         int index = 0;
         double score = 0;
         for(int i = 0;i < gestures.size();i++){
             Prediction result = gestures.get(i);
             if(result.score > score){
                 index = i;
                 score = result.score;
             }
         }
         String text = gestures.get(index).name;
         if(edit01.isFocused()){
             edit01.setText(edit01.getText().toString()+text);
         }else{
             edit02.setText(edit02.getText().toString()+text);

         }
     }
     public boolean onKeyDown(int keyCode, KeyEvent event){
         if(keyCode == KeyEvent.KEYCODE_EQUALS){
             button.performClick();//模拟单击
             button.requestFocus();//尝试获得焦点
         }
         return super.onKeyDown(keyCode, event);
     }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
